package com.linxz.wandnroid.presenter;



import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.presenter.BasePresenter;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.wandnroid.appui.view.CategoryArticlesView;
import com.linxz.wandnroid.module.WanAndroidModule;
import com.linxz.wandnroid.pojo.WanAndroidArticleBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2020年03月01日 16:11
 * version：v4.5.4
 * 描述：
 */
public class CategoryArticlesPresenter extends BasePresenter<CategoryArticlesView, WanAndroidModule> {
    private int cid;
    private int page=0;

    private List<WanAndroidArticleBean> articleBeans=new ArrayList<>();

    public List<WanAndroidArticleBean> getArticleBeans() {
        return articleBeans;
    }

    public CategoryArticlesPresenter(CategoryArticlesView mView, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(mView, lifecycleSubject);
    }

    @Override
    public WanAndroidModule createModel(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        return new WanAndroidModule(lifecycleSubject);
    }

    public void refreshData(int cid){
        this.cid=cid;
        mModel.getCategoryArticles(cid, 0, new ModelCallBack<List<WanAndroidArticleBean>>() {
            @Override
            public void onSuccess(List<WanAndroidArticleBean> model) {
                page=1;
                articleBeans.clear();
                articleBeans.addAll(model);
                if (articleBeans.size()==0){
                    mView.onDataEmpty(true);
                }else{
                    mView.onLoadSuccess();
                }
            }

            @Override
            public void onErrors(String code, String msg) {
                mView.onLoadError(msg,articleBeans.size()==0);
            }
        });

    }

    public void loadMoreData(){
       mModel.getCategoryArticles(cid, page, new ModelCallBack<List<WanAndroidArticleBean>>() {
           @Override
           public void onSuccess(List<WanAndroidArticleBean> model) {
               articleBeans.addAll(model);
               if (model==null || model.size()==0){
                   mView.onDataEmpty(false);
               }else{
                   mView.onLoadSuccess();
                   page++;
               }
           }

           @Override
           public void onErrors(String code, String msg) {
               mView.onLoadError(msg,false);
           }
       });
    }
}
